package mcia_gui;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/*
 * LogOn.java - to logging user
 *
 * @author MCIA
 */
public class LogOn extends javax.swing.JFrame {

    private DatabaseEss db;                                                     //connection to db
    private String dbName = "mcia";                                             //name of db we are connecting to
    public static String globalUsername = "";                                          //global var for username string

    public LogOn() {
        initComponents();

        db = new DatabaseEss(dbName);

        if (dbEmpty()) {
            System.out.println("DB empty - making new structure.");
            makeNewDBStructure();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        ClosingConfirmation = new javax.swing.JFrame();
        jPanel5 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        OK = new javax.swing.JButton();
        Cancel = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        usernameField = new javax.swing.JTextField();
        passwordField = new javax.swing.JPasswordField();
        submitButton = new javax.swing.JToggleButton();
        quitButton = new javax.swing.JButton();

        ClosingConfirmation.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        ClosingConfirmation.setTitle("Quit Application?");
        ClosingConfirmation.setAlwaysOnTop(true);
        ClosingConfirmation.setBounds(new java.awt.Rectangle(500, 350, 0, 0));
        ClosingConfirmation.setMaximizedBounds(new java.awt.Rectangle(300, 300, 232, 97));
        ClosingConfirmation.setMinimumSize(new java.awt.Dimension(232, 110));
        ClosingConfirmation.setName("closeConfirmation"); // NOI18N
        ClosingConfirmation.setResizable(false);

        jPanel5.setPreferredSize(new java.awt.Dimension(320, 97));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText("Do you really want to quit?");

        OK.setText("Quit");
        OK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKActionPerformed(evt);
            }
        });

        Cancel.setText("Cancel");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(OK, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Cancel))
                    .addComponent(jLabel5))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addGap(14, 14, 14)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OK)
                    .addComponent(Cancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout ClosingConfirmationLayout = new javax.swing.GroupLayout(ClosingConfirmation.getContentPane());
        ClosingConfirmation.getContentPane().setLayout(ClosingConfirmationLayout);
        ClosingConfirmationLayout.setHorizontalGroup(
            ClosingConfirmationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ClosingConfirmationLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
        ClosingConfirmationLayout.setVerticalGroup(
            ClosingConfirmationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ClosingConfirmationLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("MCIA LightControl");
        setBounds(new java.awt.Rectangle(200, 20, 800, 600));
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setText("User: Not Logged In");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 774, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
        );

        jPanel3.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Login to the system");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 784, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("Username:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setText("Password:");

        usernameField.setFont(new java.awt.Font("Tahoma", 0, 14));
        usernameField.setName(""); // NOI18N
        usernameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                usernameFieldKeyPressed(evt);
            }
        });

        passwordField.setFont(new java.awt.Font("Tahoma", 0, 14));
        passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passwordFieldKeyPressed(evt);
            }
        });

        submitButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        submitButton.setText("Submit");
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });
        submitButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                submitButtonKeyPressed(evt);
            }
        });

        quitButton.setText("Quit");
        quitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(664, 664, 664)
                .addComponent(quitButton, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                .addGap(22, 22, 22))
            .addGroup(layout.createSequentialGroup()
                .addGap(228, 228, 228)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(passwordField)
                    .addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE))
                .addGap(270, 270, 270))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(352, Short.MAX_VALUE)
                .addComponent(submitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(334, 334, 334))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(138, 138, 138)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(submitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 147, Short.MAX_VALUE)
                .addComponent(quitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
        try {
            checkCrescendials();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(LogOn.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_submitButtonActionPerformed

    private void submitButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_submitButtonKeyPressed
    }//GEN-LAST:event_submitButtonKeyPressed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
    }//GEN-LAST:event_formKeyPressed

    private void usernameFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usernameFieldKeyPressed
        if (evt.getKeyCode() == 10) {
            this.submitButtonActionPerformed(null);
        }
    }//GEN-LAST:event_usernameFieldKeyPressed

    private void passwordFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordFieldKeyPressed
        if (evt.getKeyCode() == 10) {
            this.submitButtonActionPerformed(null);
        }
    }//GEN-LAST:event_passwordFieldKeyPressed

    private void OKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKActionPerformed

        this.dispose();
        System.exit(0);
}//GEN-LAST:event_OKActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        ClosingConfirmation.dispose(); // hide closingConfirmation
}//GEN-LAST:event_CancelActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        ClosingConfirmation.setVisible(true);
    }//GEN-LAST:event_formWindowClosing

    private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitButtonActionPerformed
        ClosingConfirmation.setVisible(true);
    }//GEN-LAST:event_quitButtonActionPerformed

    private boolean dbEmpty() {
        try {
            db.stmt = db.conn.createStatement();
            ResultSet results = db.stmt.executeQuery("SELECT * FROM users WHERE name = 'admin'");
            if (results.next()) {
                db.stmt.close();
                return false;
            } else {
                db.stmt.close();
                return true;
            }
        } catch (SQLException ex) {
            return true;
        } catch (Exception ex2) {
            return true;
        }
    }

    private void makeNewDBStructure() {
        //if we need we make new db structure (if db doesn't exist)
        //first we create table users
        try {
            db.stmt = db.conn.createStatement();
            String query = "CREATE TABLE users "
                    + "("
                    + "ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "
                    + "NAME VARCHAR(80) not null, "
                    + "PASSWORD VARCHAR(100) not null"
                    + ")";
            db.stmt.executeUpdate(query);
            db.stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(LogOn.class.getName()).log(Level.SEVERE, null, ex);
        }
        //we insert admin with default password: admin (hashed)
        try {
            db.stmt = db.conn.createStatement();
            String query = "INSERT INTO users (NAME, PASSWORD) VALUES ('admin', 'D033E22AE348AEB5660FC2140AEC35850C4DA997')";
            int results_insert = db.stmt.executeUpdate(query);
            if (results_insert == 0) {
                System.out.println("ERROR: admin account not inserted!");
            } else {
            }
            db.stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(LogOn.class.getName()).log(Level.SEVERE, null, ex);
        }
        //we create units table
        try {
            db.stmt = db.conn.createStatement();
            String query = "CREATE TABLE units "
                    + "("
                    + "ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "
                    + "FLOOR INTEGER not null, "
                    + "CL INTEGER not null, "
                    + "LIGHT INTEGER not null, "
                    + "DESCRIPTION VARCHAR(300), "
                    + "IMAGE BLOB(2147483647), "
                    + "PX INTEGER default 0 not null, "
                    + "PY INTEGER default 0 not null, "
                    + "BRIGHTNESS NUMERIC(5) default 0 not null"
                    + ")";
            db.stmt.executeUpdate(query);
            db.stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(LogOn.class.getName()).log(Level.SEVERE, null, ex);
        }
        //we create local_scenes table
        try {
            db.stmt = db.conn.createStatement();
            String query = "CREATE TABLE local_scenes "
                    + "("
                    + "ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "
                    + "NAME VARCHAR(100), "
                    + "DESCRIPTION VARCHAR(500)"
                    + ")";
            db.stmt.executeUpdate(query);
            db.stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(LogOn.class.getName()).log(Level.SEVERE, null, ex);
        }
        //we create local_memberships table
        try {
            db.stmt = db.conn.createStatement();
            String query = "CREATE TABLE local_memberships "
                    + "("
                    + "ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "
                    + "LS_ID INTEGER, "
                    + "BRIGHTNESS INTEGER, "
                    + "UNIT_FLOOR NUMERIC(5) not null, "
                    + "UNIT_CL NUMERIC(5) not null, "
                    + "UNIT_LIGHT NUMERIC(5) not null"
                    + ")";
            db.stmt.executeUpdate(query);
            db.stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(LogOn.class.getName()).log(Level.SEVERE, null, ex);
        }
        //we create global_scenes table
        try {
            db.stmt = db.conn.createStatement();
            String query = "CREATE TABLE global_scenes "
                    + "("
                    + "ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "
                    + "NAME VARCHAR(100) NOT NULL, "
                    + "DESCRIPTION VARCHAR(500)"
                    + ")";
            db.stmt.executeUpdate(query);
            db.stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(LogOn.class.getName()).log(Level.SEVERE, null, ex);
        }
        //we create local_memberships table
        try {
            db.stmt = db.conn.createStatement();
            String query = "CREATE TABLE global_memberships "
                    + "("
                    + "ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "
                    + "GS_ID INTEGER not null, "
                    + "LS_NAME VARCHAR(100) not null, "
                    + "LS_FLOOR NUMERIC(5), "
                    + "LS_CL NUMERIC(5), "
                    + "TIME TIME not null "
                    + ")";
            db.stmt.executeUpdate(query);
            db.stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(LogOn.class.getName()).log(Level.SEVERE, null, ex);
        }
        return;
    }

    private void checkCrescendials() throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        String input = String.valueOf(passwordField.getPassword());
        md.update(input.getBytes());
        byte[] output = md.digest();
        String currentHashedPass = bytesToHex(output);
        String currentUsername = usernameField.getText();
        System.out.println("Current password hashed: " + currentHashedPass);

        if (currentUsername.isEmpty() || passwordField.getPassword().length == 0) {
            //no username and password provided
            JOptionPane.showMessageDialog(this, "Please type in username and password.", "No username or/and password!", JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            //accessing db
            db.stmt = db.conn.createStatement();
            ResultSet results = db.stmt.executeQuery("SELECT * FROM users WHERE name = '" + currentUsername + "'");
            if (results.next()) {

                String passFromDB = results.getString("PASSWORD");

                if (passFromDB.equals(currentHashedPass)) {
                    System.out.println("Password is correct with this in database.");

                    //setting global username
                    globalUsername = currentUsername;
                    if (currentUsername.equals("admin")) {
                        //admin logs in, we show next screen
                        AdmWelcome AdmWelcomeScreen = new AdmWelcome();
                        Main.changeFrame(AdmWelcomeScreen);
                    } else {
                        //manager logs in, we show next screen
                        ManagerWelcome MngWelcomeScreen = new ManagerWelcome();
                        Main.changeFrame(MngWelcomeScreen);
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "Password does not match with username!", "Password incorrect.", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } else {
                //no such username
                JOptionPane.showMessageDialog(this, "No such user!", "Username incorrect.", JOptionPane.ERROR_MESSAGE);
                return;
            }
            results.close();

        } catch (SQLException ex) {
            Logger.getLogger(AdmManageAccounts.class.getName()).log(Level.SEVERE, null, ex);
        }
        input = "---";
    }

    public static String bytesToHex(byte[] b) {
        char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        StringBuilder buf = new StringBuilder();
        for (int j = 0; j < b.length; j++) {
            buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
            buf.append(hexDigit[b[j] & 0x0f]);
        }
        return buf.toString();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //setting Windows Look and Feel
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.out.println("Error setting native LAF: " + ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    //new AdmWelcome().setVisible(true);
                    LogOn thisFrame = new LogOn();
                    String[] vars = null;
                    Main.main(vars);
                    Main.changeFrame(thisFrame);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(AdmWelcome.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(AdmWelcome.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancel;
    private javax.swing.JFrame ClosingConfirmation;
    private javax.swing.JButton OK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JButton quitButton;
    private javax.swing.JToggleButton submitButton;
    private javax.swing.JTextField usernameField;
    // End of variables declaration//GEN-END:variables
}
